<?php

    if (!defined('EXPONENT')) exit('');

    global $db;
    $json = null;

    $config = $db->selectObject('linklistmodule_config',"location_data='".serialize($loc)."'");
    if ($config == null) {
        $config->orderby = 'rank';
        $config->orderhow = 0; 
        $config->force_title="";
        $config->item_per_page=5;
        $config->item_at_firstpage=5;
        $config->force_top_sentence=0;
    }                             
    $max_item =  $config->item_per_page;     
    $count_post = $db->countObjects('linklist_link',"location_data='".serialize($loc)."'");
    $page_count=floor($count_post / $max_item);
    if ($count_post % $max_item != 0)
        $page_count++;      

    for ($i = 0; $i <= $page_count; $i++) 
    {        
        $page=$i+1;    
        if (($page<1) || ($page>$page_count)) $page=1;
        $start_pos=($page-1)*$max_item;
        $sortby = "";    
        switch ($config->orderhow) {
            case 0:
                $sortby = " ORDER BY name ASC ";
                break;
            case 1:
                $sortby = " ORDER BY name DESC ";
                break;
            case 2:
                $sortby = " ORDER BY RAND() ";
                break;
            case 3:
                $sortby = " ORDER BY rank ASC ";
                break;
            case 4:
                $sortby = " ORDER BY rank DESC ";
                break;
        }

        $limit="LIMIT {$start_pos},{$max_item}";
        $links = $db->selectObjects('linklist_link',"location_data='".serialize($loc)."' {$sortby} {$limit} ");

        foreach ($links as $key=>$value){
            if ($value->file_id <= 0) {
                $value->picpath = '';
            } else {
                $file = $db->selectObject('file', 'id='.$value->file_id);
                $value->picpath = "/".$file->directory.'/'.$file->filename;
                $links[$key]=$value;
            }        
        }
        $json_temp = new template('linklistmodule','_footer_slide_detail',$loc);
        $json_temp->assign('links',$links);
        $json_temp->assign('first',$i==0?1:0);
        $json_temp->assign('last',$i==($page_count-1)?1:0);
        $json[$i]->html=$json_temp->render();   
        $json[$i]->type="html";     
    }

    $template = new template('linklistmodule','_footer_slide',$loc);
    $template->assign('slides',json_encode($json));
    $template->assign('config',$config);
    $template->register_permissions(
    array('administrate','configure','create','edit','delete'),$loc);
    $template->output();    
?>
